import {observable, action, toJS} from 'mobx';
import {  Bubble,Tools } from 'weaver-mobile';
import * as Api from '../../api/UserCenterApi';
import * as BlogApi from '../../api/BlogAttentionApi';
import UserShareSetStore from './UserShareSetStore';
import MyFansStore from './MyFansStore';
import MsgRemindStore from './MsgRemindStore';
import SettingStore from './SettingStore';
import { ListViewPage } from 'weaver-mobile-page';
const getLabel = Tools.getLabel;
export default class UserCenterStore {
  @observable userShareSetStore = new UserShareSetStore();
  @observable myFansStore = new MyFansStore();
  @observable msgRemindStore = new MsgRemindStore();
  @observable settingStore = new SettingStore();
  @observable isLoading = true;
	@observable userInfo = {}; //当前用户信息
  @observable menuCountData = {}; //统计数字信息
  
	@observable visitorList = []; //最新5条 最近来访列表
  @observable accessList = []; //最新5要 最近访问列表
  
  @observable visitorType = null; //最近访问页面当前类型-access:最近访问;visitor:最近来访
  @observable visitorsList = new ListViewPage.Store({ route: '/userCenter/visitor' }); //来访页面列表信息
  @observable accessPageList = new ListViewPage.Store({ route: '/userCenter/access' }); //来访页面列表信息
  @observable visitorCurrentPage = 1; //展示列表当前页
  @observable accessCurrentPage = 1; //展示列表当前页

  @observable gzbroswercondition = ''; //人力资源浏览按钮 过滤已经关注人员


  @action getFilterids = () => {
      BlogApi.outAttentionUsers().then(result => {
          this.gzbroswercondition = result.ids;
          this.userInfo.allowRequest = result.allowRequest;
          this.userInfo.isenableecme = result.isenableecme;
      })
          .catch(error => {

          });
  }


 @action  batchAttention = (ids) => {
    BlogApi.batchAttention({ids:ids}).then(result => {
     if(result.status=='1'){
         Bubble.success(getLabel(513699,'请求关注发送成功！') , 1);
     }else if(result.status=='3'){
        
     }else {
         Bubble.error(getLabel(513700,'请求关注发送失败！') );
     }
    })
  }

  @action setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
  }

  // 页面初始化数据
	@action
	initData() {

		this.initCurrentUserInfo();
		this.initMenuCountData();
        this.getVisitorList();
        this.getAccessList();
        this.getFilterids();
  }

  @action('当前用户信息')
	initCurrentUserInfo = () => {
		Api.getCurrentUserInfo().then((res) => {
      this.userInfo = res.currentUser;
		});
	}
  
  @action('统计数字')
	initMenuCountData = (callback) => {
		Api.getMenuCountData().then((res) => {
      this.menuCountData = res.countData;
      this.isLoading = false;
            if(callback)callback(res)
		});


	}

  @action('修改统计信息')
  setMenuCountData = (params) =>{
    Object.assign(this.menuCountData, params);
  }
  
  // 最近访问、来访页面信息
  @action('最近来访列表请求')
  getVisitorList = () => {
    let params = {
      currentPage: this.visitorCurrentPage++,
      pageSize: 20,
      recordType: 'visit',
    }
    Api.getVisitorList(params).then((res) => {
      const { list, status, totalSize, currentPage, pageSize } = res;
      if (status === '1') {
        const newState = {
          isTable: false,
          loading: false,
          haveCheck: true,
          template: {default: []},
          hasMore: totalSize > currentPage * pageSize,
          data: [...(currentPage === 1 ? [] : this.visitorsList.data), ...list],
        };
        this.visitorsList.setState(newState);
        this.visitorsList.updateDataSource();
        if (currentPage === 1) this.visitorList = list.slice(0,5);
      }
    });
  }

  @action('最近访问')
  getAccessList = () => {
    let params = {
      currentPage: this.accessCurrentPage++,
      pageSize: 20,
      recordType: 'access',
    }
    Api.getVisitorList(params).then((res) => {
      const { list, status, totalSize, currentPage, pageSize } = res;
      if (status === '1') {
        const newState = {
          isTable: false,
          loading: false,
          haveCheck: true,
          template: {default: []},
          hasMore: totalSize > currentPage * pageSize,
          data: [...(currentPage === 1 ? [] : this.accessPageList.data), ...list],
        };
        this.accessPageList.setState(newState);
        this.accessPageList.updateDataSource();
        if (currentPage === 1) this.accessList = list.slice(0,5);
      }
		});
  }

  getFirstList = (page) => {
    if (page === 'visitor' ) {
      this.visitorCurrentPage = 1;
      this.getVisitorList();
    } else {
      this.accessCurrentPage = 1;
      this.getAccessList();
    }
  }

  userClear = () => {
    this.currentPage = 1;
  }
}